//获取页面元素
var error_tips = document.querySelector('#error')
var u_name = document.querySelector("input[name='username']")
var password = document.querySelector("input[name='password']")
var sex = document.querySelectorAll("input[type='radio']")
var password_again = document.querySelector("input[name='pw_again']")
var phone = document.querySelector("input[name='phone']")
var phone_check = document.querySelector("input[name='U-phone-check']")
var submit = document.querySelector("input[value='注册']")
var radio = document.querySelector(".radio_box")
var spend_phone = document.querySelector("input[name='spend_phone']")
var to_top = document.querySelector(".register_tips")

// 发送短信验证码
spend_phone.addEventListener('click', function () {
    //发送验证码...
    alert("验证码已发送，请注意查看!")
})

// 点击注册提示按钮返回顶部
to_top.addEventListener('click', function () {
    //把内容滚动指定的像素数（第一个参数是向右滚动的像素数，第二个参数是向下滚动的像素数）
    window.scrollBy(0, -835);
})

//判断是否为中文
function isChinese(temp){
    var re=/[^\u4E00-\u9FA5]/;
    if (re.test(temp)) return false ;
    return true ;
}
// 顶部注册提示信息控制
submit.addEventListener('click', function (e) {
    var flag = 0
    //1.判断信息是否完整
    if (u_name.value == '') {
        u_name.style.borderColor = "#d26f5c";
        flag = 1
    } else {
        u_name.style.borderColor = '#6d5ed4'
    }
    if (password.value == '' || password_again.value == '') {
        password.style.borderColor = "#d26f5c";
        password_again.style.borderColor = "#d26f5c";
        flag = 1
    } else {
        password_again.style.borderColor = '#6d5ed4'
        password.style.borderColor = '#6d5ed4'
    }
    if (phone.value == '') {
        phone.style.borderColor = "#d26f5c";
        flag = 1
    } else {
        phone.style.borderColor = '#6d5ed4'
    }
    if (phone_check.value == '') {
        phone_check.style.borderColor = "#d26f5c";
        flag = 1
    } else {
        phone_check.style.borderColor = '#6d5ed4'
    }
    if (!sex[0].checked && !sex[1].checked) {
        radio.style.borderColor = '#d26f5c';
        flag = 1
    } else {
        radio.style.borderColor = '#6d5ed4'
    }
    //若信息不完整，在tips框进行提示
    if (flag == 1) {
        error_tips.innerHTML = "请将信息填写完整哦!"
        return
    } else {
        error_tips.innerHTML = '欢迎，等您好久啦!'
    }
    //判断用户名是否为中文
    if(isChinese(u_name.value)){
        error_tips.innerHTML = "抱歉，目前用户名只能为英文，我们还在努力！"
        to_top.style.opacity = 1
        return
    }else{
        to_top.style.opacity = 0
    }
    //2.两次密码是否正确
    if (password.value != password_again.value) {
        error_tips.innerHTML = "两次输入的密码不一样哦!"
        to_top.style.opacity = 1
        return
    }else{
        to_top.style.opacity = 0
    }
    //3.发送请求给后端
    var username = u_name.value
    var password_value = password.value
    var sex_value = sex[0].checked ?  "boy": "gril"
    var phone_value = phone.value
    //请求
    $.ajax({
        url: "user/register",
        type:"post",
        data:JSON.stringify({
            username:username,
            password:password_value,
            sex:sex_value,
            phone:phone_value
        }),
        contentType:"application/json;charset=UTF-8",
        dataType:"json",
        success:function (data){
            if(data.username == "none"){
                alert("您输入的用户名/电话已被注册!")
            }else{
                alert("注册成功!")
                location.href = "/chapter12/login"
            }

        }
    })
})

// 点击返回事件
var back = document.querySelector(".back")
back.addEventListener('click',function (){
    history.back()
})